package com.clayecloud.module_login.ui.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.Observable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.launcher.ARouter;
import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.module_login.R;
import com.clayecloud.module_login.databinding.FragmentPhoneLoginBinding;
import com.clayecloud.module_login.entity.LoginUser;
import com.clayecloud.module_login.ui.dialog.BaseDialog;
import com.clayecloud.module_login.ui.dialog.MenuDialog;
import com.clayecloud.module_login.viewModels.LoginActivityViewModel;
import com.gyf.immersionbar.ImmersionBar;
import com.hjq.bar.OnTitleBarListener;

import java.util.ArrayList;
import java.util.List;

public class PhoneLoginFragment extends Fragment implements ToastAction {


    private FragmentPhoneLoginBinding fragmentPhoneLoginBinding;
    private LoginActivityViewModel loginActivityViewModel;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentPhoneLoginBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_phone_login, container,false);
        ViewModelProvider.Factory factory = ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication());
        ViewModelProvider viewModelProvider = new ViewModelProvider(getActivity(),factory);
        loginActivityViewModel = viewModelProvider.get(LoginActivityViewModel.class);   //获取到主Activity 的ViewModel（数据层）

        fragmentPhoneLoginBinding.setLoginUser(loginActivityViewModel);

        //头部导航栏事件监听
        fragmentPhoneLoginBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                getActivity().finish();
                getActivity().overridePendingTransition(R.anim.window_fake, R.anim.window_bottom_out);
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {
                List<String> data = new ArrayList<>();
                data.add("找回密码");
                data.add("人工申诉账号找回");
                // 底部选择框
                new MenuDialog.Builder(fragmentPhoneLoginBinding.getRoot().getContext())
                        .setList(data)
                        .setListener(new MenuDialog.OnListener<String>() {
                            @Override
                            public void onSelected(BaseDialog dialog, int position, String string) {
                                toast("位置：" + position + "，文本：" + string);
                            }

                            @Override
                            public void onCancel(BaseDialog dialog) {
                                //取消按钮事件处理
                                //toast("取消了");
                            }
                        })
                        .show();
            }
        });

        //点击之后 切换Activity的ViewModel
        fragmentPhoneLoginBinding.TextViewAccountLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               MutableLiveData<Integer> int_currentPager = loginActivityViewModel.getCurrentPager();
               int_currentPager.setValue(0);
               loginActivityViewModel.setCurrentPager(int_currentPager);
            }
        });

        fragmentPhoneLoginBinding.register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build("/login/register/index")
                        .withTransition(R.anim.window_right_in, R.anim.window_fake)
                        .navigation(fragmentPhoneLoginBinding.getRoot().getContext());
            }
        });

        //监听ediText的变化进行监听
        loginActivityViewModel.getLoginUserMutableLiveData().getValue().addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                buttonStateChanger();
            }
        });

        //底部微信登录方式
        fragmentPhoneLoginBinding.loginWithWeChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("暂时停止提供微信登录方式");
            }
        });

        //底部QQ登录方式
        fragmentPhoneLoginBinding.loginWithQQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("暂时停止提供QQ登录方式");
            }
        });

        //监听切页的变化进行刷新
        loginActivityViewModel.getCurrentPager().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                buttonStateChanger();
            }
        });

        //登录按钮的监听
        fragmentPhoneLoginBinding.loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //手机验证码登录
            }
        });

        return fragmentPhoneLoginBinding.getRoot();
    }

    /**
     * 用来处理创建除view的所有东西
     * @param savedInstanceState
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //沉浸式状态栏
        ImmersionBar.with(this).statusBarDarkFont(true).init();
    }

    /**
     * 在删除此fragment时 进行操作防止内存泄漏
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        // 必须调用该方法，防止内存泄漏
        ImmersionBar.destroy(this);
    }

    /**
     * 判断是否更新按钮的状态
     */
    private void buttonStateChanger(){
        fragmentPhoneLoginBinding.loginButton.setEnabled(false);
        LoginUser loginUser = loginActivityViewModel.getLoginUserMutableLiveData().getValue();
        if (loginUser.getStr_phone() == null){
            return;
        }
        if (!"".equals(loginUser.getStr_phone()) && loginUser.isBool_isRead() && loginActivityViewModel.getCurrentPager().getValue() == 1){
            fragmentPhoneLoginBinding.loginButton.setEnabled(true);
        }
    }

}
